package org.hilo.boot.core.logback;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 
 * @author zollty
 * @since 2018年4月20日
 */
public class SensitiveInfoUtils {
    /**
     * [姓名] 只显示第一个汉字，其他隐藏为星号<例子：李**>
     * 
     * @param fullName
     * @return
     */
    public static String chineseName(String fullName) {
        if (isEmpty(fullName)) {
            return "";
        }
        StringBuilder sbu = new StringBuilder();
        sbu.append(fullName.charAt(0));
        for (int i = 1; i < fullName.length(); i++) {
            sbu.append('*');
        }
        return sbu.toString();
    }

    /**
     * [身份证号] 显示最后四位，其他隐藏。共计18位或者15位。<例子：*************5762>
     * 
     * @param idCardNum
     * @return
     */
    public static String idCardNum(String idCardNum) {
        if (isEmpty(idCardNum)) {
            return "";
        }
        StringBuilder sbu = new StringBuilder();
        for (int i = 0; i < idCardNum.length() - 4; i++) {
            sbu.append('*');
        }
        sbu.append(idCardNum.substring(idCardNum.length() - 4, idCardNum.length()));
        return sbu.toString();
    }

    /**
     * [手机号码] 前三位，后四位，其他隐藏<例子:138******1234>
     * 
     * @param num
     * @return
     */
    public static String mobilePhone(String num) {
        if (isEmpty(num)) {
            return "";
        }
        StringBuilder sbu = new StringBuilder();
        sbu.append(num.substring(0, 3));
        for (int i = 0; i < num.length() - 7; i++) {
            sbu.append('*');
        }
        sbu.append(num.substring(num.length() - 4, num.length()));
        return sbu.toString();
    }

    /**
     * [银行卡号] 前六位，后四位，其他用星号隐藏每位1个星号<例子:6222600**********1234>
     * 
     * @param cardNum
     * @return
     */
    public static String bankCard(String cardNum) {
        if (isEmpty(cardNum)) {
            return "";
        }
        StringBuilder sbu = new StringBuilder();
        sbu.append(cardNum.substring(0, 7));
        for (int i = 0; i < cardNum.length() - 10; i++) {
            sbu.append('*');
        }
        sbu.append(cardNum.substring(cardNum.length() - 4, cardNum.length()));
        return sbu.toString();
    }
    
    public static boolean isEmpty(CharSequence str) {
        return str == null || str.length() == 0;
    }
    
    public static String matchAndReplace(String target, Pattern pattern, int type) {
        Matcher match = pattern.matcher(target);
        boolean result = match.find();
        if (!result) {
            return target;
        }
        StringBuffer newStr = new StringBuffer();
        while (result) { // 如果匹配成功就替换
            switch (type) {
            case 0:
                match.appendReplacement(newStr, SensitiveInfoUtils.mobilePhone(match.group()));
                break;
            case 1:
                match.appendReplacement(newStr, SensitiveInfoUtils.idCardNum(match.group()));
                break;
            case 2:
                match.appendReplacement(newStr, SensitiveInfoUtils.bankCard(match.group()));
                break;
            default:
                break;
            }
            result = match.find(); // 继续下一步匹配
        }
        match.appendTail(newStr);
        return newStr.toString();
    }
}
